package com.ita.acmsystem.merchant.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ita.acmsystem.merchant.dao.IMerchantDao;
import com.ita.acmsystem.merchant.dao.impl.MerchantDaoImpl;
import com.ita.acmsystem.merchant.entity.Merchant;

/**
 * Servlet implementation class AjaxMerchantStatusServlet
 */
public class AjaxMerchantStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	IMerchantDao dao = new MerchantDaoImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AjaxMerchantStatusServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doJson(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private void doJson(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession();
		Merchant merchant = (Merchant) session.getAttribute("user");
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		
		System.out.println("查询用户是否被拉黑。。。");
		if (merchant != null) {
			String id = merchant.getMerchantId();
			merchant = dao.queryById(id);
			session.setAttribute("user", merchant);
		} else {
			merchant = new Merchant();
			merchant.setMerchantBlanklist(0);
		}	
		
		Map<String, String> map = new HashMap<String, String>();
		if ("1".equals(String.valueOf(merchant.getMerchantBlanklist()))) {
			map.put("status", "error");
			map.put("msg", "你已被拉黑");
		} else {
			map.put("status", "success");
		}
		ObjectMapper mapper = new ObjectMapper();
		PrintWriter writer = response.getWriter();
		writer.write(mapper.writeValueAsString(map));
	}
}
